<template>
  <div class="container">
    <text class="big-title">示例</text>
    <div class="content">
      <div style="flex-direction: row">
        <div class="btn" @click="get"><text class="btn-text">GET</text></div>
        <div class="btn" style="margin-left: 30px;" @click="post"><text class="btn-text">POST</text></div>
      </div>
      <div class="divider"></div>
      <textarea class="textarea" rows="10" :value="result"></textarea>
    </div>
    <text class="big-title">文档</text>
    <div class="button" @click="showDoc"><text class="btn-text">点击查看文档</text></div>
  </div>
</template>

<style scoped>
.container {
  padding: 10px;
}
.content {
}
.btn {
  border-width: 2px;
  border-radius: 8px;
  border-color: blue;
  padding-left: 30px;
  padding-right: 30px;
  padding-top: 15px;
  padding-bottom: 15px;
  background-color: transparent;
}
.btn:active {
  background-color: #ECECEC;
}
.divider {
  height: 1.8px;
  background-color: #ECECEC;
  margin-top: 20px;
  margin-bottom: 20px;
}
.textarea {
  border-width: 2px;
  border-color: black;
  padding: 10px;
}
.big-title {
  color: black;
  font-size: 50px;
  font-weight: bold;
  margin-bottom: 20px;
  margin-top: 20px;
}
.button {
  height: 60px;
  justify-content: center;
  opacity: 1.0;
}
.button:active {
  opacity: 0.6;
}
.btn-text {
  font-size: 30px;
  color: blue;
}
</style>

<script>
  let stream = weex.requireModule('stream');
  let modal = weex.requireModule('modal');
  let navigator = weex.requireModule('navigator');
  import utils from '../utils.js';
  export default {
    data: {
      result: ''
    },
    methods: {
      get() {
        stream.fetch({
          url: 'https://app.yubo725.top/moments?offset=0&pagesize=1',
          type: 'json',
          method: 'GET'
        }, (res) => {
          if (res.ok) {
            modal.toast({message: 'success', duration: 1});
            this.result = res.data;
          } else {
            modal.toast({message: 'fail', duration: 1});
            this.result = JSON.stringify(res);
          }
        })
      },
      post() {
        stream.fetch({
          url: 'https://app.yubo725.top/search',
          type: 'json',
          method: 'POST',
          body: this.toParams({
            key: '王'
          })
        }, (res) => {
          if (res.ok) {
            modal.toast({message: 'success', duration: 1});
            this.result = res.data;
          } else {
            modal.toast({message: 'fail', duration: 1});
            this.result = JSON.stringify(res);
          }
        })
      },
      toParams(obj) {
        var param = "";
        for(const name in obj) {
          if(typeof obj[name] != 'function') {
            param += "&" + name + "=" + encodeURI(obj[name]);
          }
        }
        return param.substring(1);
      },
      showDoc() {
        navigator.push({
          url: utils.getDocDetailUrl(weex.config.bundleUrl),
          animated: true
        });
      }
    }
  }
</script>
